package com.aabte.commons.encrypt.rsa;

/**
 * @author Daniel
 * @version 1.0
 * @date 2020/3/28
 */
public class EncryptorRSAFactory {

  private static RSAPrivateKeyEncryptor privateEncryptor;
  private static RSAPublicKeyEncryptor publicEncryptor;

  private static final Object SERVER_LOCK = new Object();
  private static final Object CLIENT_LOCK = new Object();

  public static RSAPrivateKeyEncryptor getPrivateEncryptor() {

    if (null == privateEncryptor) {
      synchronized (SERVER_LOCK) {
        if (null == privateEncryptor) {
          privateEncryptor = new RSAPrivateKeyEncryptor();
        }
      }
    }
    return privateEncryptor;
  }

  public static RSAPublicKeyEncryptor getPublicEncryptor() {
    if (null == publicEncryptor) {
      synchronized (CLIENT_LOCK) {
        if (null == publicEncryptor) {
          publicEncryptor = new RSAPublicKeyEncryptor();
        }
      }
    }
    return publicEncryptor;
  }
}
